package com.x.example.demo;

import android.content.Context;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.x.example.toolbar.ToolbarInitializer;
import com.x.example.toolbar.ToolbarRegistry;
import com.x.example.toolbar.items.TitleItem;
import com.x.example.toolbar.tabpages.HomePage;
import com.x.example.toolbar.tabpages.MessagePage;
import com.x.example.toolbar.tabpages.UserCenterPage;
import com.example.uiframework.utils.Utils;
import com.x.uikit.page.XPage;
import com.x.uikit.toolbar.XToolbar;
import com.x.uikit.toolbar.XToolbarItem;

import java.util.ArrayList;
import java.util.List;

public class ArticleToolbarDemo extends XPage {
    private XToolbar mBottomToolbar;
    private XToolbar mTopToolbar;
    private ViewModel mViewModel = new ViewModel();
    private WebView mWebView;

    public ArticleToolbarDemo(@NonNull Context context) {
        super(context);
        LinearLayout contentView = new LinearLayout(getContext());
        contentView.setOrientation(LinearLayout.VERTICAL);
        this.addView(contentView);
        mTopToolbar = new XToolbar(getContext());
        mTopToolbar.setItemCreator(ToolbarRegistry.get());
        mTopToolbar.setPadding(Utils.res.dpI(12), 0, Utils.res.dpI(12), 0);
        mTopToolbar.setItems(mViewModel.getTitleTokens());
        TitleItem titleItem = mTopToolbar.findItem(ToolbarInitializer.TOKEN_TITLE, TitleItem.class);
        titleItem.setSingleLine(true);
        LinearLayout.LayoutParams lpTitle = (LinearLayout.LayoutParams) titleItem.getLayoutParams();
        lpTitle.weight = 1;
        titleItem.setLayoutParams(lpTitle);
        contentView.addView(mTopToolbar, -1, Utils.res.dpI(45));
        mTopToolbar.setItemClickListener(onItemClickListener);


        mWebView = initWebView(getContext());
        mWebView.loadUrl("https://baijiahao.baidu.com/s?id=1726377026963350960&wfr=spider&for=pc");
        LinearLayout.LayoutParams lpPages = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        lpPages.weight = 1;
        contentView.addView(mWebView, lpPages);
        View divider = new View(getContext());
        divider.setBackgroundColor(0xff999999);
        LinearLayout.LayoutParams lpDivider = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, Utils.res.dpI(0.5f));
        contentView.addView(divider, lpDivider);
        mBottomToolbar = new XToolbar(getContext());
        mBottomToolbar.setItemCreator(ToolbarRegistry.get());
        LinearLayout.LayoutParams lpToolbar = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, Utils.res.dpI(50));
        contentView.addView(mBottomToolbar, lpToolbar);
        mBottomToolbar.setItems(mViewModel.getTokens());
        mBottomToolbar.setItemClickListener(onItemClickListener);
    }

    XToolbar.OnItemClickListener onItemClickListener = new XToolbar.OnItemClickListener() {
        @Override
        public void onItemClicked(String token, XToolbarItem item) {
            if (TextUtils.equals(token, ToolbarInitializer.TOKEN_BACK)) {
                onBackPressed();
            } else {
                Toast.makeText(mBottomToolbar.getContext(), String.format("点击了%s", token), Toast.LENGTH_SHORT).show();
            }
        }
    };

    private WebView initWebView(Context context) {
        WebView webView = new WebView(context);
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                TitleItem titleItem = mTopToolbar.findItem(ToolbarInitializer.TOKEN_TITLE, TitleItem.class);
                if (titleItem != null) {
                    titleItem.setTitle("正在努力加载。。。");
                }
            }

            @Override
            public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
                super.onReceivedError(view, request, error);
            }
        });
        webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
                TitleItem titleItem = mTopToolbar.findItem(ToolbarInitializer.TOKEN_TITLE, TitleItem.class);
                if (titleItem != null) {
                    titleItem.setTitle(title);
                }
            }
        });
        return webView;
    }


    private static class ViewModel {
        private List<String> mTokens = new ArrayList<>();
        private List<String> mTitleTokens = new ArrayList<>();


        public ViewModel() {
            mTokens.add(ToolbarInitializer.TOKEN_SPIT15);
            mTokens.add(ToolbarInitializer.TOKEN_BACK);
            mTokens.add(ToolbarInitializer.TOKEN_COMMENT);
            mTokens.add(ToolbarInitializer.TOKEN_COLLECT);
            mTokens.add(ToolbarInitializer.TOKEN_SPIT15);
            mTokens.add(ToolbarInitializer.TOKEN_LIKE);
            mTokens.add(ToolbarInitializer.TOKEN_SPIT15);
            mTokens.add(ToolbarInitializer.TOKEN_SHARE);
            mTokens.add(ToolbarInitializer.TOKEN_SPIT15);
            mTitleTokens.add(ToolbarInitializer.TOKEN_BACK);
            mTitleTokens.add(ToolbarInitializer.TOKEN_SPIT15);
            mTitleTokens.add(ToolbarInitializer.TOKEN_TITLE);
            mTitleTokens.add(ToolbarInitializer.TOKEN_SPIT15);
            mTitleTokens.add(ToolbarInitializer.TOKEN_SHARE);
        }

        public List<String> getTitleTokens() {
            return mTitleTokens;
        }

        public View getPage(Context context, String token) {
            if (TextUtils.equals(token, ToolbarInitializer.TOKEN_HOME)) {
                return new HomePage(context);
            } else if (TextUtils.equals(token, ToolbarInitializer.TOKEN_X)) {
                return new XPage(context);
            } else if (TextUtils.equals(token, ToolbarInitializer.TOKEN_MESSAGE)) {
                return new MessagePage(context);
            } else if (TextUtils.equals(token, ToolbarInitializer.TOKEN_USER_CENTER)) {
                return new UserCenterPage(context);
            }
            return null;
        }

        public int getCount() {
            return mTokens.size();
        }

        public String getToken(int position) {
            if (position > mTokens.size() - 1 || position < 0) {
                return null;
            }
            return mTokens.get(position);
        }

        public int getPosition(String token) {
            return mTokens.indexOf(token);
        }

        public List<String> getTokens() {
            return mTokens;
        }
    }

    @Override
    public boolean onBackPressed() {
        if (mWebView.canGoBack()) {
            mWebView.goBack();
            return true;
        }
        return super.onBackPressed();
    }
}
